<template>
  <view v-if="cart.length!==0" class="cart-container">
    <my-address></my-address>
    <!-- 购物车商品列表的标题区域 -->
    <view class="cart-title">
      <!-- 左侧的图标 -->
      <uni-icons type="shop" size="18"></uni-icons>
      <!-- 描述文本 -->
      <text class="cart-title-text">购物车</text>
    </view>
    <uni-swipe-action>
      <block v-for="(goods,idx) in cart " :key="goods.goods_id">
        <uni-swipe-action-item @click="swipeActionClickHandler($event,goods)" :right-options="options">
          <my-goods @click-box="gotoGoodsDetail" @num-change="numChangeHandler" @radio-change="radioCchangeHandler"
            showRadio showNum :goods="goods">
          </my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
    <my-settle></my-settle>
  </view>
  <my-empty v-else></my-empty>
</template>

<script>
  // 导入自己封装的 mixin 模块
  import badgeMix from '@/mixins/tabbar-badge.js'
  // 导入
  import { mapState, mapMutations } from 'vuex'
  export default {
    // 将 badgeMix 混入到当前的页面中进行使用
    mixins: [badgeMix],
    computed: {
      ...mapState('m_cart', ['cart'])
    },
    data() {
      return {
        options: [{
          text: '删除',
          style: {
            backgroundColor: '#c00000'
          }
        }]
      };
    },
    methods: {
      ...mapMutations('m_cart', ['changeGoodsState', 'changeGoodsCount', 'removeGoodsById']),
      radioCchangeHandler(e) {
        this.changeGoodsState(e)
      },
      numChangeHandler(e) {
        this.changeGoodsCount(e)
      },
      // 点击了滑动操作按钮
      swipeActionClickHandler(e, goods) {
        if (e.content.text === '删除') {
          this.removeGoodsById(goods.goods_id)
        }
      },
      gotoGoodsDetail(e) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + e.goods_id
        })
      }
    }
  }
</script>

<style lang="scss">
  .cart-container {
    padding-bottom: 100rpx;
  }

  .cart-title {
    height: 40px;
    display: flex;
    align-items: center;
    font-size: 14px;
    padding-left: 5px;
    border-bottom: 1px solid #efefef;

    .cart-title-text {
      margin-left: 10px;
    }
  }
</style>
